package smoker.tools;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.*;
import smoker.server.*;

public class PhotoPanel extends JWindow {
	private static final long serialVersionUID = 1L;
	private JTable table;
	private IUserManager iUser;

	public PhotoPanel(IUserManager iUser) {
		this.iUser = iUser;
		this.setSize(500, 300);
		this.setAlwaysOnTop(true);
		init();
	}

	private void init() {
		String[] header = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
		table = new JTable();
		table.setModel(new Tablemode(GUIManager.headerIcon(), header));
		for (int i = 0; i < header.length; i++) {
			TableColumn column = table.getColumn(header[i]);
			column.setCellRenderer(new MyTableCellRenderer());
		}
		table.setRowHeight(45);
		JScrollPane sp = new JScrollPane(table);
		add(sp);
		sp.addMouseListener(new MouseAdapter() {
			 public void mouseExited(MouseEvent e) {
				 dispose();
			 }
		});
		table.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				if(e.getButton() != MouseEvent.BUTTON1){
					return;
				}
				if(e.getClickCount() == 2){
					iUser.icon((ImageIcon)table.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
					dispose();
				}
			}
		});
	}

	class MyTableCellRenderer extends JLabel implements TableCellRenderer {
		private static final long serialVersionUID = 1L;
		private final Color borderLineColor = new Color(139, 168, 198);
		Border border;

		MyTableCellRenderer() {
			setOpaque(true);
			border = BorderFactory.createLineBorder(borderLineColor);
			this.setPreferredSize(new Dimension(160, 42));
		}

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			if (isSelected) {
				this.setBorder(border);
			} else {
				this.setBorder(null);
			}
			setIcon((ImageIcon) value);
			return this;
		}
	}

	class Tablemode extends DefaultTableModel {
		private static final long serialVersionUID = 1L;
		public Tablemode(ImageIcon[][] values, String[] header) {
			super(values, header);
		}
		public boolean isCellEditable(int row, int column) {
			return false;
		}
	}
}
